package com.ofhb.webapp.client;

import java.util.ArrayList;
import java.util.Random;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.ofhb.webapp.client.base.AppFrame;
import com.ofhb.webapp.client.ui.grid.GridData;
import com.ofhb.webapp.client.ui.grid.GridDataType;
import com.ofhb.webapp.client.utils.DataCache;
import com.ofhb.webapp.client.utils.LoadingAnimation;
import com.ofhb.webapp.client.utils.ServiceUtil;
import com.ofhb.webapp.shared.dto.LookupDataDTO;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class AdminManager implements EntryPoint {
	
	private RootPanel rootPanel;
	private LoadingAnimation loadingAnimation = new LoadingAnimation();
	
	public void onModuleLoad() {
		
		rootPanel = RootPanel.get("_main");

		getLookupDataFromServer();
	}
	
	
	private void getLookupDataFromServer() {
		loadingAnimation.show();
		AsyncCallback<LookupDataDTO> callback = new AsyncCallback<LookupDataDTO>() {
			
			@Override
			public void onSuccess(LookupDataDTO result) {
				loadingAnimation.hide();
				DataCache.lookupDataDTO = result;

				AppFrame appFrame = new AppFrame();
				rootPanel.add(appFrame);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				loadingAnimation.hide();
				Window.alert("onFailure : " + caught.getMessage());
			}
		};
		
		ServiceUtil.LOOKUP_SERVICE.getLookupData(callback);
	}
	
	
	public static GridData getGridData() {
		Random rand = new Random();
		String names[] = new String[] { "Suraj", "Raja", "Sandy", "Amol", "Jyasa" };
		String cell[] = new String[] { "9923934533", "9850863455", "9422064356", "9960423445", "9123475765" };

		GridData gridData = new GridData();
		for (int i = 0; i < 22; i++) {
			ArrayList<String> colValues = new ArrayList<String>();
			colValues.add(String.valueOf(i));
			colValues.add(names[rand.nextInt(4)]+cell[rand.nextInt(4)]);
			colValues.add(names[rand.nextInt(4)]);
			colValues.add(names[rand.nextInt(4)]);
			colValues.add(cell[rand.nextInt(4)]);
			gridData.addRow(colValues);
		}

		gridData.addColumn("id", 0, GridDataType.ID);
		gridData.addColumn("User Name", 175, GridDataType.TEXT);
		gridData.addColumn("First Name", 100, GridDataType.TEXT);
		gridData.addColumn("Last Name", 100, GridDataType.TEXT);
		gridData.addColumn("Contact Number", 200, GridDataType.NUMBER);

		return gridData;
	}
}
